package cn.jltx.abslistviewadapterdemo.ExpandableBase;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.jltx.abslistviewadapterdemo.R;
import cn.jltx.abslistviewadapterdemo.ViewHolderUtils;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/05/04.
 */

public class ExpandableAdapter extends BaseExpandableListAdapter{
    private Context context;
    private List<GroupBean> groupBeanList;

    public ExpandableAdapter(Context context, List<GroupBean> groupBeanList) {
        this.context = context;
        this.groupBeanList = groupBeanList;
    }

    @Override
    public int getGroupCount() {
        if(groupBeanList != null){
            return groupBeanList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(groupBeanList != null && groupPosition >=0 && groupPosition < groupBeanList.size()){
            GroupBean groupBean = groupBeanList.get(groupPosition);
            if(groupBean != null){
                return groupBean.getChildList().size();
            }
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if(groupBeanList != null && groupPosition >=0 && groupPosition < groupBeanList.size()){
            return groupBeanList.get(groupPosition);
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if(groupBeanList != null && groupPosition >=0 && groupPosition < groupBeanList.size()){
            GroupBean groupBean = groupBeanList.get(groupPosition);
            if (groupBean != null) {
                List<ItemBean> itemBeanList = groupBean.getChildList();
                if(itemBeanList != null && childPosition >=0 && childPosition < itemBeanList.size()){
                    return itemBeanList.get(childPosition);
                }
            }
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView =  inflater.inflate(R.layout.group_layout,parent,false);
        }
        TextView textView = ViewHolderUtils.get(convertView,R.id.parent_tv);
        GroupBean groupBean = groupBeanList.get(groupPosition);
        if(groupBean != null){
            textView.setText(groupBean.getGroup() != null ? groupBean.getGroup() : "");
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.child_layout,parent,false);
        }
        TextView textView = ViewHolderUtils.get(convertView,R.id.child_tv);
        ImageView imgIcon = ViewHolderUtils.get(convertView,R.id.img_icon);
        GroupBean groupBean = groupBeanList.get(groupPosition);
        if(groupBean != null && groupBean.getChildList() != null){
            ItemBean itemBean =  groupBean.getChildList().get(childPosition);
            textView.setText(itemBean.getiName() != null ? itemBean.getiName() : "");
            if(itemBean.getiId() != 0){
                imgIcon.setImageResource(itemBean.getiId());
            }
        }
        return convertView;
    }

    /**
     *指定子View是否可选择.true表示可选择，false表示不可选择
     * @param groupPosition
     * @param childPosition
     * @return
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
//        if(groupPosition ==0 && childPosition == 0){
//            return false;
//        }
        return true;
    }
}
